﻿using UnityEngine;
using System.Collections;

public class Tip : Object, ICanSwim {
	
	public Mob mob;

	//public int barIndex;
	public Bar bar;
	public int barloc;
	public static int tiptimer = 2048*4;
	public int tiptime = 0;

	public static bool isdance;


	public void create() {
		mob = new Mob ();
		mob.create ("Tip");
	}
	
	public void init(Bar a, int _barloc = 0) {
		bar = a;				
		mob.init(Tappa.game.data.tip);
		mob.pos = a.data.beerStart.pos;
		barloc = _barloc;
		tiptime = 0;
		mob.upd ();		
	}
	
	public void upd() {
		int len = bar.data.barLengthScale;
		xy pos;
		xy pos1 = bar.data.beerStart.pos;
		xy pos2 = bar.data.beerEnd.pos;
		float tween = (float)barloc / len;
		pos.x = (int)Mathf.Lerp (pos1.x, pos2.x, tween);
		pos.y = (int)Mathf.Lerp (pos1.y, pos2.y, tween);;		
		mob.pos = pos;
		mob.r.sortingOrder = barloc + len;
		mob.upd();

		if (bar == Tappa.game.bars [Tappa.game.itsame.bar]) {
			float dist = barloc - Tappa.game.itsame.runpos;
			//dist = Mathf.Abs (dist);
			if (dist < 1) {
				Score.add (Score.Value.Tip);
				isAlive = false;
				Peep.startDance();
			}
		}

		tiptime++;
		if (tiptime > tiptimer)	isAlive = false;
		
	}
	
	public bool isAlive = false;
	public bool wantsToGoBackInThePool() {
		return !isAlive;
	}
	public void getInThePool() {
		isAlive = false;
		mob.go.SetActive (false);
	}
	public void getOutOfThePool() {
		isAlive = true;
		mob.go.SetActive (true);
	}
	
}
